<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.talkweb.ncfw.web.common.WebAttributeConst" %>
<%@ page import="org.springframework.security.core.AuthenticationException" %>
<%@ page import="com.talkweb.ncfw.web.common.MyWebAttributeConst" %>
<%@ page import="org.apache.commons.codec.binary.Hex"%>
<%@ page import="java.security.interfaces.RSAPublicKey"%>
<%@ page import="com.talkweb.ncfw.utils.encrypt.RSAUtils"%>
<%@ page import="com.talkweb.ncfw.security.SecurityHelper"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
	if (SecurityHelper.getCurrentUser() != null) {%>
		<script type="text/javascript">
		<!--
			window.location.href = './index.jsp';
		//-->
		</script>
	<%}
	String path 	= request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	String info		= request.getParameter("info");
	String lastLoginUserName = (String)session.getAttribute(WebAttributeConst.LOGIN_LAST_USERNAME_KEY);
	if (lastLoginUserName == null) {
		lastLoginUserName = "";
	}
	Object modulus	= session.getAttribute(MyWebAttributeConst.PUBLIC_KEY_MODULUS);
	Object exponent	= session.getAttribute(MyWebAttributeConst.PUBLIC_KEY_EXPONET);
	if (modulus == null || exponent == null) {
		RSAPublicKey publicKey = RSAUtils.getDefaultPublicKey();
		modulus		= new String(Hex.encodeHex(publicKey.getModulus().toByteArray()));
		exponent	= new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray()));
		session.setAttribute(MyWebAttributeConst.PUBLIC_KEY_MODULUS, modulus);
		session.setAttribute(MyWebAttributeConst.PUBLIC_KEY_EXPONET, exponent);
	}
 %>
<title>远程抄表系统</title>
<style type="text/css">
	html{ border:0; height:100%;}
	body{ margin:0; padding:0; height:100%; background:#f9f9f9; font-size:12px; font-family:Arial;}
	input { width:160px; height:18px; line-height:18px; border:1px solid #5fa2be; padding:3px;}
	.box{ width:100%; height:100%;}
	.box td{ vertical-align: middle; }
	.cdiv{ width:1004px; height:479px;margin:0 auto; background:url(<%=basePath%>foundation/images/bg.jpg) no-repeat center center; color:#fff; border: solid 2px #e5e5e5;}
	.cdiv .login { height:100px; width:280px; float:left; color:#004b91; font-weight: bold;  padding:140px 0 0 120px;
	}
	.cdiv .login a{color: #666666;text-decoration: underline;font-weight: normal;}
	.cdiv .foot{margin:510px 0 0 0;  text-align:center; color:#333333;}

</style>
<script type="text/javascript" src="<%=basePath%>foundation/js/RSA.js"></script>  
<script type="text/javascript" src="<%=basePath%>foundation/js/BigInt.js"></script>  
<script type="text/javascript" src="<%=basePath%>foundation/js/Barrett.js"></script> 
<script type="text/javascript">
<!--
var info = '<%=info%>';
if (info == 'AuthenticationFailure') {
	alert('登录失败, 登录帐号与密码不一致或登录帐号失效.');
}

function onLogin () {
	var username = document.getElementById('j_username');
	var password = document.getElementById('password');
	if (username.value == '') {
		alert('登录帐号不能为空!');
		username.focus();
		return;
	}
	if (password.value == '') {
		alert('密码不能为空!');
		password.focus();
		return;
	}
	setMaxDigits(130);
	var key 			= new RSAKeyPair('<%=exponent%>', '', '<%=modulus%>');
	var pwd 			= password.value;
	var encrptPassword	= encryptedString(key, pwd);
	document.getElementById('j_password').value = encrptPassword;
	document.getElementById('loginForm').submit();
}

function onReset () {
	document.getElementById('loginForm').reset();
}

//-->
</script>
</head>

<body>

<form method="post" id="loginForm" action="<%=path%>/j_spring_security_check">
	<table class="box"><tr><td>
	<div class="cdiv">
	  <div class="login"><table width="99%" border="0" cellspacing="14" cellpadding="0">
	  <tr>
	    <td align="right">登录帐号<br /></td>
	    <td align="left">
	      <input type="text" name="j_username" id="j_username" style="height:22px; width: 120px" value="sysadmin"/>
	    </td>
	  </tr>
	  <tr>
	    <td align="right">密  &nbsp;&nbsp;码</td>
	    <td align="left"><input type="password" name="password" id="password" style="height:22px; width: 120px"  value="000000"/></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
	        <tr>
	          <td><img src="foundation/images/sub.gif" width="56" height="20" onclick="javascript: onLogin();"/></td>
	          <td></td>
	          <td>&nbsp;</td>
	        </tr>
	      </table></td>
	  </tr>
	</table></div>
	
	</div>
	</td></tr></table>
    <input type="hidden" name="j_password" id="j_password" value=""/>
</form>
</body>
</html>